<template>
  <el-dialog
      v-model="isShowDialog"
      title="上传文件"
      width="400"
  >
    <div class="content-wrap">
      <el-form :model="ruleForm" :rules="rules"
               ref="ruleFormRef" size="default" label-width="80px">
        <el-row :gutter="35">
          <el-col :span="24" class="mb20">
            <el-form-item label="文件夹" prop="name">
              <el-input v-model="ruleForm.name" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="上传文件" prop="name">
              <Uploader ref="uploaderRef" :key="autoKey" :limit="1" mode="jccloud" :isCloud="isCloud" :keepLocal="isCloud ? 0 : 1"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template #footer>
        <span class="dialog-footer">
            <el-button @click="onCancel" size="default">取 消</el-button>
            <el-button type="primary" @click="onSubmit" size="default"
                       :loading="loading">上传</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import {reactive, toRefs, defineComponent, ref, nextTick, unref} from 'vue';
import {ElMessage} from "element-plus";
import {addFile} from "/@/api/attachment";
import Uploader from "/@/components/uploader/index.vue";

export default defineComponent({
  name: 'attachmentDirectoryEdit',
  components: {Uploader},
  setup(props, {emit}) {
    const ruleFormRef = ref<HTMLElement | null>(null);
    const uploaderRef = ref();
    const autoKey = ref("xx34xx3dw4e4xxx4xxs");
    const state = reactive({
      loading: false,
      isShowDialog: false,
      isCloud: 0,
      ruleForm: {
        id: undefined,
        name: ''
      },
      groups: [],
      rules: {
        name: [
          {required: true, message: "文件夹名称不能为空", trigger: "blur"},
        ]
      }
    });
    const openDialog = (_row: any, _source: string) => {
      initForm();
      autoKey.value = new Date().getTime() + (Math.random() * 1000).toFixed(0);
      nextTick(() => {
        state.isCloud = _source =="local" ? 0 : 1;
        state.ruleForm.id = _row.directoryId;
        state.ruleForm.name = _row.directoryName;
      })
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 新增
    const onSubmit = () => {
      const formWrap = unref(ruleFormRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          const _list = uploaderRef.value.getFileList();
          if(_list.length > 0) {
            addFile({
              directoryId: state.ruleForm.id,
              fileHash: _list[0].name,
            }).then(() => {
              ElMessage.success('上传文件成功');
              closeDialog(); // 关闭弹窗
              emit('callback')
            }).finally(() => {
              state.loading = false;
            })
          } else {
            ElMessage.error('请正确上传文件');
          }
        }
      })
    };
    const initForm = () => {
      state.ruleForm = {
        id: undefined,
        name: ''
      }
    };
    return {
      autoKey,
      uploaderRef,
      ruleFormRef,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      ...toRefs(state),
    };
  },
});
</script>
<style lang="scss" scoped>

</style>
